package com.xinzhidi.controller;

import com.aliyun.oss.model.ObjectListing;

import com.xinzhidi.common.core.domain.AjaxResult;
import com.xinzhidi.common.utils.SecurityUtils;
import com.xinzhidi.config.AliyunConfig;
import com.xinzhidi.model.FileBusiness;
import com.xinzhidi.service.FileBusinessService;
import com.xinzhidi.util.OSSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * 阿里云文件上传
 *
 * @author cx
 * @date 2023/4/23 10:28
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/aliYunFile")
@Api("阿里云文件上传控制器")
public class AliYunFileController {

    private final FileBusinessService businessService;


    /**
     * 多图片上传
     *
     * @param fileList
     * @return
     */
    @PostMapping(value = "/uploads")
    @ResponseBody
    @ApiOperation("多图片上传")
    public AjaxResult checkList(List<MultipartFile> fileList, Integer businessType) {
        OSSUtil ossClient = new OSSUtil();
        FileBusiness fileBusiness = null;
        List<FileBusiness> list = new ArrayList<>();
        try {
            for (int i = 0; i < fileList.size(); i++) {
                //将文件上传
                fileBusiness = ossClient.uploadImg2Oss(fileList.get(i), businessType);
                fileBusiness.setCreateBy(SecurityUtils.getUserId());
                fileBusiness.setUpdateBy(SecurityUtils.getUserId());
                list.add(fileBusiness);
            }
            businessService.saveOrUpdateBatch(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success(list);
    }

    /**
     * 单文件上传
     *
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/upload")
    @ResponseBody
    @ApiOperation("单文件上传")
    public AjaxResult uploadPhoto(@RequestParam("file") MultipartFile file, Integer businessType) {
        OSSUtil ossClient = new OSSUtil();
        if (file.isEmpty()) {
            return AjaxResult.error("上传文件不能为空");
        }
        String newsUrl = "";
        FileBusiness fileBusiness = null;
        if (!(file).isEmpty()) {
            try {
                //将文件上传
                fileBusiness = ossClient.uploadImg2Oss(file, businessType);
                fileBusiness.setCreateBy(SecurityUtils.getUserId());
                fileBusiness.setUpdateBy(SecurityUtils.getUserId());
                businessService.save(fileBusiness);
                //获取文件的URl地址  以便前台  显示
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            AjaxResult.error("上传图片不可为空");
        }
        return AjaxResult.success(fileBusiness);
    }

    /**
     * 获取oss文件
     *
     * @param key
     * @return
     */
    @RequestMapping(value = "/onlineSee", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("获取oss文件")
    public Map<String, Object> onlineSee(String key) {
        OSSUtil ossClient = new OSSUtil();
        Map<String, Object> m = new HashMap<>();
        String strings = ossClient.onlineSee(key);
        m.put("url", strings);
        return m;
    }

    /**
     * 文档预览
     *
     * @param key
     * @return
     */
    @RequestMapping(value = "/onlinePreview", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("文档预览")
    public Map<String, Object> onlinePreview(String key) {
        OSSUtil ossClient = new OSSUtil();
        Map<String, Object> m = new HashMap<>();
        Object strings = ossClient.onlinePreview(key);
        m.put("url", strings);
        return m;
    }

    /**
     * 获取文件
     *
     * @param key
     * @return
     */
    @RequestMapping(value = "/getUrl", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("获取文件")
    public Map<String, Object> getUrl(String key) {
        OSSUtil ossClient = new OSSUtil();
        Map<String, Object> m = new HashMap<>();
        String strings = ossClient.getUrl(key);
        m.put("url", strings);
        return m;
    }

    /**
     * 获取文件夹
     *
     * @param fileName
     * @return
     */
    @RequestMapping(value = "/fileFolder", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("获取文件夹")
    public Map<String, Object> fileFolder(String fileName) {
        Map<String, Object> m = new HashMap<>();
        OSSUtil ossClient = new OSSUtil();
        List<String> strings = ossClient.fileFolder(fileName);
        m.put("fileFolder", strings);
        return m;
    }

    /**
     * 通过文件名下载文件
     *
     * @param objectName    文件全路径
     * @param localFileName
     * @return
     */
    @RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
    @ResponseBody
    public void downloadFile(HttpServletResponse response, String objectName, String localFileName) {
        try {
            OSSUtil ossClient = new OSSUtil();
            ossClient.downloadFile(response, objectName, localFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除文件
     *
     * @param key
     * @return
     */
    @RequestMapping(value = "/deleteObject", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("删除文件")
    public void deleteObject(String key) {
        try {
            OSSUtil ossClient = new OSSUtil();
            ossClient.delFile(key);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 获取文件夹下文件列表
     *
     * @param fileHost
     * @return
     */
    @RequestMapping(value = "/listFile", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("获取文件夹下文件列表")
    public Map<String, Object> listFile(String fileHost, String nextMarker) {
        OSSUtil ossClient = new OSSUtil();
        Map<String, Object> m = new HashMap<>();
        List<String> strings = ossClient.listFile(fileHost, nextMarker);
        m.put("listFile", strings);
        return m;
    }

    /**
     * 获取文件夹下文件列表和文件夹
     *
     * @param fileHost
     * @return
     */
    @RequestMapping(value = "/listFile2", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("获取文件夹下文件列表和文件夹")
    public Map<String, Object> listFile2(String fileHost, String nextMarker) {
        OSSUtil ossClient = new OSSUtil();
        Map<String, Object> m = new HashMap<>();
        ObjectListing strings = ossClient.listFile2(fileHost, nextMarker);
        m.put("listFile", strings);
        return m;
    }

    /**
     * 创建文件夹
     *
     * @param folder
     * @return
     */
    @RequestMapping(value = "/createFolder", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("创建文件夹")
    public Map<String, Object> createFolder(String folder) {
        Map<String, Object> m = new HashMap<>();
        try {
            OSSUtil ossClient = new OSSUtil();
            String reFolder = ossClient.createFolder(folder);
            m.put("reFolder", reFolder);
        } catch (Exception e) {
            e.printStackTrace();
            m.put("error", "请输入文件名");
        }
        return m;
    }

    /**
     * 后台提供获取secretAccessKey，accessKeyId接口
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/ossConfig", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("后台提供获取secretAccessKey，accessKeyId接口")
    public Map<String, Object> ossConfig() {
        Map<String, Object> m = new HashMap<>();
        try {
            String aliyunAccessKey = AliyunConfig.accessKey;
            String aliyunAccessId = AliyunConfig.accessId;
            m.put("aliyunAccessKey", aliyunAccessKey);
            m.put("aliyunAccessId", aliyunAccessId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return m;
    }
}
